using AnyCAD.Foundation;
using System;

namespace AnyCAD.Modeling.Sphere
{
    public class SphereModel : ElementModel
    {
        public GPnt Center = new GPnt();
        public double Diameter = 10;

        public TopoShape CreateShape()
        {
            return ShapeBuilder.MakeSphere(Center, Diameter/2);
        }

        public override  void SaveState()
        {
            var schema = ElementSchemaManager.Instance().FindSchema(nameof(SphereModel));
            schema.GetParameterDict().Add(nameof(Diameter), Diameter);
        }
    }

    class SphereSchema : ElementSchema
    {
        public SphereSchema()
            : base(nameof(SphereModel))
        {
            this.SetTitle("球");
            this.SetCategory("Solid");
            SetIcon("/Rapid.Common.Res;component/Image/SectionBar.png");
        }

        public override ElementModel CreateModel()
        {
            return new SphereModel();
        }

        public override string OnGetInstanceClassName()
        {
            return nameof(ShapeElement);
        }

        public override bool OnParameterChanged(Document doc, SchemaComponent properties)
        {
            var element = ShapeElement.Cast(properties.GetEntity());
            if (element == null) 
                return false;

            SphereModel model = new SphereModel();
            model.Load(properties);

            var shape = model.CreateShape();

            element.SetShape(shape);

            return true;
        }
    }
}
